package com.boc.ewr.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boc.ewr.bcimp.entity.Cac;
import com.boc.ewr.bcimp.entity.CollCallList;
import com.boc.ewr.bcimp.service.ICacService;
import com.boc.ewr.bcimp.service.ICollCallListService;
import com.boc.ewr.business.entity.CaCusmCallOlfdbk;
import com.boc.ewr.business.mapper.CaCusmCallOlfdbkMapper;
import com.boc.ewr.business.service.ICaCusmCallOlfdbkService;
import com.boc.ewr.util.file.enumeration.CheckNeedEnum;
import com.boc.ewr.util.file.enumeration.CheckTypeEnum;
import com.boc.ewr.util.file.enumeration.CusmFdbkTagEnum;
import com.boc.ewr.util.file.enumeration.FdbkStatusEnum;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @Description: 线上反馈的客户会话明细分类表
 * @Author: jeecg-boot
 * @Date: 2023-04-20
 * @Version: V1.0
 */
@Service
public class CaCusmCallOlfdbkServiceImpl extends ServiceImpl<CaCusmCallOlfdbkMapper, CaCusmCallOlfdbk> implements ICaCusmCallOlfdbkService {


    @Autowired
    private ICollCallListService collCallListService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Autowired
    private ICacService cacService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createInfo() {
        /*
         * 1)筛出“手机号码”、“标签”、“机器人”三列内容，去除空白值以及“机器人”列中的
         * 	您好，我是"中国银行""银企对账中心"的，您这里是*吗？字段，留下客户名称。
         * 2)使用“手机号码”、“机器人”中的客户名称与CAC中的“对账联系人手机”、“客户名称”匹配
         * 	二级行名称、机构号、余额证对单所属的客户号、余额证对单的账单号生成客户会话明细表。
         * 3)按照客户反馈标签关键字，“库-已销户、库-不会操作、库-质疑骗子、库-欲投诉、
         * 	核对单位-拒绝、通知账单-拒绝、通知账单-账户未使用、库-U盾要重办、对账人已离职、
         * 	核对单位-非本人、开场白挂断、三次未接通”形成“客户会话明细分类表”
         * 4)需要增加对账联系人和其手机号码
         * */
        //清空当前记录
        this.remove(null);

        //生成数据
        boolean result = true;
        List<CollCallList> collCallLists = collCallListService.list();
        for (CollCallList collCallList : collCallLists) {

            boolean contains = collCallList.getRobotWord().contains("您好，我是\"中国银行\"\"银企对账中心\"的");
            if (!contains){
                continue;
            }

            // 筛选数据
            String phoneNum = collCallList.getPhoneNum();
            String callTag = collCallList.getCallTag();
            String cusmNm = "";

            String robotWord = collCallList.getRobotWord();
            // 创建正则表达式
            String regex = ".*您这里是(.+?)吗\\？.*";
            // 匹配字串
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(robotWord);
            if (matcher.matches()) {
                // 获取匹配的客户姓名
                cusmNm = matcher.group(1);
            }

            // 与Cac匹配数据
            List<Cac> cacList = cacService.list(
                    new LambdaQueryWrapper<Cac>()
                            .eq(Cac::getLinkerMobile, phoneNum)
                            .eq(Cac::getCusmName, cusmNm)
            );

            // 生成客户会话明细表
            List<CaCusmCallOlfdbk> caCusmCallDtlListInsert = new ArrayList();
            for (Cac cac : cacList) {
                CaCusmCallOlfdbk caCusmCallOlfdbk = new CaCusmCallOlfdbk();
                // 二级行名称
                caCusmCallOlfdbk.setBranchNm(
                        sysBaseAPI.selectAllById(
                                cac.getParentBranchNo()
                        ).getDepartName()
                );
                caCusmCallOlfdbk.setBranchNo(cac.getBranchNo());
                caCusmCallOlfdbk.setCusmNo(cac.getAccCusmNo());
                caCusmCallOlfdbk.setCaNo(cac.getCaNo());
                // 匹配标签
                List<String> stringList = Arrays.asList(callTag.split(","));
                List<String> cusmTagValueList = Arrays.asList(CusmFdbkTagEnum.values())
                        .stream()
                        .map(CusmFdbkTagEnum::getValue)
                        .collect(Collectors.toList());
                for (String singleTag : stringList) {
                    if (cusmTagValueList.contains(singleTag)){
                        caCusmCallOlfdbk.setCusmFdbkTag(singleTag);
                        // 设置核实类别与核实要求
                        CaCusmCallOlfdbk caCusmCallOlfdbkReturn = this.setTypeAndNeed(caCusmCallOlfdbk, collCallList.getCallTag());
                        caCusmCallOlfdbkReturn.setFdbkSta(FdbkStatusEnum.NOFEEDBACK.getIndex());

                        caCusmCallDtlListInsert.add(caCusmCallOlfdbk);
                    }
                }
            }
            boolean b = this.saveBatch(caCusmCallDtlListInsert);
            if (!b) {
                result = b;
                break;
            }
        }
        return result;
    }

    // 设置核实类别与核实要求
    private CaCusmCallOlfdbk setTypeAndNeed(CaCusmCallOlfdbk caCusmCallOlfdbk, String cusmFdbkTag) {

        String checkType = "";
        String checkNeed = "";
        if (cusmFdbkTag.equals(CusmFdbkTagEnum.UNABLEOOPERATE.getIndex())) {
            checkType = CheckTypeEnum.OPERATE.getIndex();
            checkNeed = CheckNeedEnum.ISOPERATE.getIndex();
        } else if (
                cusmFdbkTag.equals(CusmFdbkTagEnum.REPROCESSING.getIndex())
                        || cusmFdbkTag.equals(CusmFdbkTagEnum.DEPART.getIndex())
                        || cusmFdbkTag.equals(CusmFdbkTagEnum.DEPARTMENTNOSELF.getIndex())
        ) {
            checkType = CheckTypeEnum.MODIFY.getIndex();
            checkNeed = CheckNeedEnum.ISMODIFY.getIndex();
        } else if (cusmFdbkTag.equals(CusmFdbkTagEnum.CANCELLATION.getIndex())) {
            checkType = CheckTypeEnum.CANCELLATION.getIndex();
            checkNeed = CheckNeedEnum.ISCANCELLATION.getIndex();
        } else if (
                cusmFdbkTag.equals(CusmFdbkTagEnum.LIAR.getIndex())
                        || cusmFdbkTag.equals(CusmFdbkTagEnum.COMPLAINT.getIndex())
                        || cusmFdbkTag.equals(CusmFdbkTagEnum.DEPARTMENTREFUSE.getIndex())
                        || cusmFdbkTag.equals(CusmFdbkTagEnum.BILLREFUSE.getIndex())
                        || cusmFdbkTag.equals(CusmFdbkTagEnum.BILLNOUSE.getIndex())
        ) {
            checkType = CheckTypeEnum.QUESTION.getIndex();
            checkNeed = CheckNeedEnum.REASON.getIndex();
        } else if (
                cusmFdbkTag.equals(CusmFdbkTagEnum.HANGUP.getIndex())
                        || cusmFdbkTag.equals(CusmFdbkTagEnum.THREETIMES.getIndex())
        ) {
            checkType = CheckTypeEnum.COLLECTION.getIndex();
            checkNeed = null;
        }
        caCusmCallOlfdbk.setCheckType(checkType);
        caCusmCallOlfdbk.setCheckNeed(checkNeed);
        return caCusmCallOlfdbk;
    }
}
